const db = require('../db/index')

// bcrypt 加密
const bcrypt = require('bcryptjs')
// jsonwebtoken 生成 token
const jwt = require('jsonwebtoken')
// 导入配置文件
const config = require('../jwt_config')

// 注册
exports.register = (req, res) => {
  // (1) 判断前端传过来的数据是否为空
  const reginfo = req.body
  if (!reginfo.account || !reginfo.password) {
    return res.cc('账号或密码不能为空')
  }
  // (2) 查询数据库中是否有该用户
  const sql = `select * from users where account = ? `
  db.query(sql, reginfo.account, (err, results) => {
    if (err) {
      return res.cc(err)
    }
    if (results.length > 0) {
      return res.cc('该用户已存在')
    }
    // (3) 对用户密码进行加密 -- bcrypt
    reginfo.password = bcrypt.hashSync(reginfo.password, 10)
    // (4) 将加密后密码插入到数据库中
    const sql2 = `insert into users set ?`
    // 注册身份
    const identity = '用户'
    // 注册时间
    const create_time = new Date()
    db.query(sql2, {
      account: reginfo.account,
      password: reginfo.password,
      identity,
      create_time,
      // 冻结状态 
      status: 0
    }, (err, results) => {
      if (err) {
        return res.cc(err)
      }
      if (results.affectedRows !== 1) {
        return res.cc('注册失败')
      }
      res.cc('注册成功', 0)
    })
  })
}

// 登录
exports.login = (req, res) => {
  const logininfo = req.body
  const sql = `select * from users where account = ?`
  db.query(sql, logininfo.account, (err, results) => {
    if (err) return res.cc(err)
    // (1) 判断数据表中有没有 前端传过来的账号
    if (results.length !== 1) return res.cc('该用户不存在')
    // (2) 判断密码是否正确
    const compareResult = bcrypt.compareSync(logininfo.password, results[0].password)
    if (!compareResult) return res.cc('密码错误')
    // (3) 判断用户是否被冻结
    if (results[0].status === 1) return res.cc('该用户已被冻结, 请联系管理员')
    // (4) 生成 token 注意剔除掉隐私信息
    const user = { ...results[0], password: '', imgUrl: '', create_time: '', update_time: '' }
    const token = jwt.sign(user, config.secretKey, { expiresIn: config.expiresIn })
    res.send({
      status: 0,
      message: '登录成功',
      token: 'Bearer ' + token,
      results: results[0]
    })
  })
}

// 超级管理员路由
const superAdminRouter = [
  {
    name: 'home',
    path: '/home',
    meta: { title: '首页' },
    component: 'home/index'
  }, {
    name: 'set',
    path: '/set',
    meta: { title: '设置' },
    component: 'set/index'
  }, {
    name: 'overview',
    path: '/overview',
    meta: { title: '系统概览' },
    component: 'overview/index'
  }, {
    name: 'message_manager',
    path: '/message_manager',
    meta: { title: '消息管理员' },
    component: 'user_manage/message_manager/index'
  }, {
    name: 'product_manager',
    path: '/product_manager',
    meta: { title: '产品管理员' },
    component: 'user_manage/product_manager/index'
  }, {
    name: 'user_list',
    path: '/user_list',
    meta: { title: '用户列表' },
    component: 'user_manage/user_list/index'
  }, {
    name: 'user_manager',
    path: '/user_manager',
    meta: { title: '用户管理' },
    component: 'user_manage/user_manager/index'
  }, {
    name: 'product_manage',
    path: '/product_manage',
    meta: { title: '产品管理' },
    component: 'product/product_manage/index'
  }, {
    name: 'out_product_manage',
    path: '/out_product_manage',
    meta: { title: '出库管理' },
    component: 'product/out_product_manage/index'
  }, {
    name: 'message_list',
    path: '/message_list',
    meta: { title: '消息管理' },
    component: 'message/message_list/index'
  }, {
    name: 'recycle',
    path: '/recycle',
    meta: { title: '回收站' },
    component: 'message/recycle/index'
  }, {
    name: 'file',
    path: '/file',
    meta: { title: '文件管理' },
    component: 'file/index'
  }, {
    name: 'login_log',
    path: '/login_log',
    meta: { title: '登录日志' },
    component: 'login_log/index'
  }, {
    name: 'operation_log',
    path: '/operation_log',
    meta: { title: '操作日志' },
    component: 'operation_log/index'
  }
]

// 用户管理员路由
const userAdminRouter = [
  {
    name: 'home',
    path: '/home',
    meta: { title: '首页' },
    component: 'home/index'
  }, {
    name: 'set',
    path: '/set',
    meta: { title: '设置' },
    component: 'set/index'
  }, {
    name: 'user_list',
    path: '/user_list',
    meta: { title: '用户列表' },
    component: 'user_manage/user_list/index'
  }, {
    name: 'user_manager',
    path: '/user_manager',
    meta: { title: '用户管理' },
    component: 'user_manage/users_manager/index'
  }, {
    name: 'file',
    path: '/file',
    meta: { title: '文件管理' },
    component: 'file/index'
  }
]

// 产品管理员路由
const productAdminRouter = [
  {
    name: 'home',
    path: '/home',
    meta: { title: '首页' },
    component: 'home/index'
  }, {
    name: 'set',
    path: '/set',
    meta: { title: '设置' },
    component: 'set/index'
  }, {
    name: 'product_manage',
    path: '/product_manage',
    meta: { title: '产品管理' },
    component: 'product/product_manage/index'
  }, {
    name: 'out_product_manage',
    path: '/out_product_manage',
    meta: { title: '出库管理' },
    component: 'product/out_product_manage/index'
  }, {
    name: 'file',
    path: '/file',
    meta: { title: '文件管理' },
    component: 'file/index'
  }
]

// 消息管理员路由
const messageAdminRouter = [
  {
    name: 'home',
    path: '/home',
    meta: { title: '首页' },
    component: 'home/index'
  }, {
    name: 'set',
    path: '/set',
    meta: { title: '设置' },
    component: 'set/index'
  }, {
    name: 'message_list',
    path: '/message_list',
    meta: { title: '消息管理' },
    component: 'message/message_list/index'
  }, {
    name: 'recycle',
    path: '/recycle',
    meta: { title: '回收站' },
    component: 'message/recycle/index'
  }, {
    name: 'file',
    path: '/file',
    meta: { title: '文件管理' },
    component: 'file/index'
  }
]

// 普通用户路由
const userRouter = [
  {
    name: 'home',
    path: '/home',
    meta: { title: '首页' },
    component: 'home/index'
  }, {
    name: 'set',
    path: '/set',
    meta: { title: '设置' },
    component: 'set/index'
  }, {
    name: 'product_manage',
    path: '/product_manage',
    meta: { title: '产品管理' },
    component: 'product/product_manage/index'
  }, {
    name: 'out_product_manage',
    path: '/out_product_manage',
    meta: { title: '出库管理' },
    component: 'product/out_product_manage/index'
  }, {
    name: 'file',
    path: '/file',
    meta: { title: '文件管理' },
    component: 'file/index'
  }
]

// 根据用户身份获取路由 前端参数: id
exports.getRouterById = (req, res) => {
  const sql = `select identity from users where id = ?`
  db.query(sql, [req.body.id], (err, result) => {
    if (err) return res.cc(err)
    let router = []
    if (result[0].identity === '超级管理员') {
      router = superAdminRouter
    }
    if (result[0].identity == '用户管理员') {
      router = userAdminRouter
    }
    if (result[0].identity == '产品管理员') {
      router = productAdminRouter
    }
    if (result[0].identity == '消息管理员') {
      router = messageAdminRouter
    }
    if (result[0].identity == '用户') {
      router = userRouter
    }
    res.send({
      status: 0,
      message: '获取动态路由成功',
      data: router
    })

  })
}